let sql = require('../sql/sql');
let data = require('../libs/data');
let TopClient = require('../libs/socketlib/api/topClient').TopClient;
var redis = require('redis').createClient();
let activity = require('../service/activity');
var db = require('../libs/db');
var client = new TopClient({
    'appkey': '23783096',
    'appsecret': '5dfdc7c3706ad90e31defcb326108b63',
    'REST_URL': 'http://gw.api.taobao.com/router/rest'
});

module.exports = {
    start(req,res){
        console.log('------开始答题------');
        var code = req.body.activityid;
        var time = (new Date()).valueOf();
        client.execute('taobao.openim.tribe.sendmsg', {
            'user':{uid:'manager',taobao_account:false, app_key:'23783096'},
            'tribe_id':'2414402980',
            'msg':{at_flag:0,msg_content:{code:code,type:'1',time:time},msg_type:0}
        }, function(error, response) {
            if (!error){
                console.log('成功：',response);
                var data = {};

                redis.set('activityid_start_time_' + code, time, function (err) {
                    console.log(err)
                });
                redis.expire('activityid_start_time_' + code, 3600);

                data.code = '200';
                data.msg = '查询数据';
                res.status(200).send(data);
            }else{
                console.log('错误：',error);
            }
        })
    },
    // 结束答题
    end (req, res) {
        console.log('------结束答题------');
        var code = req.body.activityid;

        if (activity.updateActive(code)) {
            client.execute('taobao.openim.tribe.sendmsg', {
                'user':{uid:'manager',taobao_account:false, app_key:'23783096'},
                'tribe_id':'2414402980',
                'msg':{at_flag:0,msg_content:{code:code,type:'2'},msg_type:0}
            }, function(error, response) {
                if (!error){
                    console.log(response);
                    var data = {};
                    var time = (new Date()).valueOf();
                    redis.del('activityid_start_time_' + code, function (err) {
                        console.log(err);
                    });

                    data.code = '200';
                    data.msg = '结束活动成功';
                    res.status(200).send(JSON.stringify(data));
                }else{
                    console.log(error);
                }
            })
        } else {
            data.code = '104';
            data.msg = '结束活动失败';
            res.status(200).send(JSON.stringify(data));
        }

    },
    // 获取活动缓存
    getActivityTime (req, res) {
        console.log('------获取活动缓存------');
        var code = req.body.code;
        data = {};
        redis.get('activityid_start_time_' + code, function (err, object) {
            if (!err && object != null) {
                data.code = '200';
                data.msg = '查询成功';
                data.time = object;
                data.nowtime = Date.parse(new Date());
                res.status(200).send(data);
            }else{
                data.code = '101';
                data.msg = '查询失败';
                res.status(200).send(data);
            }
        });
    },
    //添加用户
    addUser(req,res){
        var sql = 'select * from jyj_user a where a.flag = 2 limit 150,200';
        db.sequelize.query(sql, {
            type: db.sequelize.QueryTypes.SELECT
        }).then(function (result) {
            result.forEach(function (item,i) {
                client.execute('taobao.openim.users.add', {
                    'userinfos':{userid:item.userid,password:123456}
                }, function(error, response) {
                    if (!error) console.log(response);
                    else console.log(error);
                })
            })
            data.code = '200';
            data.msg = '查询数据成功';
            data.data = result;
            res.status(200).send(JSON.stringify(data));
        })
    },
    //查询群用户
    queryTribeUser(req,res){
        client.execute('taobao.openim.tribe.getmembers', {
            'user':{uid:'manager',taobao_account:false, app_key:'23783096'},
            'tribe_id':'2414402980'
        }, function(error, response) {
            data.code = '200';
            data.msg = '查询数据成功';
            data.data = response;
            res.status(200).send(JSON.stringify(data));
        })
    },
    //群加人
    addTribeUser(req,res){
        var sql = 'select * from jyj_user a where a.flag = 2';
        db.sequelize.query(sql, {
            type: db.sequelize.QueryTypes.SELECT
        }).then(function (result) {
            result.forEach(function (item,i) {
                client.execute('taobao.openim.tribe.join', {
                    'user':{uid:item.userid,taobao_account:false, app_key:'23783096'},
                    'tribe_id':'2414402980'
                }, function(error, response) {
                    if (!error) console.log(response);
                    else console.log(error);
                })
            })
            data.code = '200';
            data.msg = '查询数据成功';
            data.data = result;
            res.status(200).send(JSON.stringify(data));
        })
    }
};